from flask import Blueprint, request
from common.libs.Helper import ops_renderJSON, ops_renderErrJSON, iPagenation, ops_render
from common.models.user import User
from application import db, app
from common.libs.DataHelper import getCurrentTime, getCurrentDate
from flask import json
import datetime
from sqlalchemy import func
import requests
import urllib.request
import flask_excel as excel

seal_page = Blueprint("seal_page", __name__)
from common.models.book_detail import BookDetail
#
#
# def pagessearch(seal, stime, etime):
#     req = request.values
#     stime = stime
#     etime = etime
#     page = 1
#     if 'p' in req and req['p']:
#         page = int(req['p'])
#
#     page_params = {
#         'total_count': seal.count(),
#         "page_size": 14,
#         'page': page,
#         'url': "/seal/htsearch?starttime=" + stime + "&endtime=" + etime,
#     }
#     pages = iPagenation(page_params)
#     # -1 - 30,30 - 60 ,60 - 90
#     offset = (page - 1) * page_params['page_size']
#     limit = page * page_params['page_size']
#     seal = seal[offset:limit]
#
#     return pages, seal
#
#
#
#
#
#
# #
# #
# def pagesadmin(seal):
#     req = request.values
#     page = 1
#     if 'p' in req and req['p']:
#         page = int(req['p'])
#
#     page_params = {
#         'total_count': seal.count(),
#         "page_size": 8,
#         'page': page,
#         'url': "/seal/htadmin?",
#     }
#     pages = iPagenation(page_params)
#     # -1 - 30,30 - 60 ,60 - 90
#     offset = (page - 1) * page_params['page_size']
#     limit = page * page_params['page_size']
#     seal = seal[offset:limit]
#
#     return pages, seal
#
#
#
# @seal_page.route('/htadmin', methods=['GET', 'POST'])
# def sealhtadmin():
#     ht = SealOrder.query.filter_by(type="合同", mark=2).order_by(SealOrder.created_time.desc())
#     pages, ht = pagesadmin(ht)
#
#     return ops_render("/admin/record.html", {"pages": pages, "ht": ht})
#
#
# @seal_page.route('/htsearch', methods=['GET', 'POST'])
# def sealhtsearch():
#     starttime = request.args.get('starttime')
#     endtime = request.args.get('endtime')
#
#     ht = SealOrder.query.filter(
#         SealOrder.created_time.between(starttime, endtime), SealOrder.type == "合同", SealOrder.mark == 2).order_by(
#         SealOrder.created_time.desc())
#
#     pages, ht = pagessearch(ht, starttime, endtime)
#
#     return ops_render("/admin/record.html", {"pages": pages, "ht": ht, "stime": starttime, "etime": endtime})
#
#
#
#
#
#
#
#
#
#
#
# #
# #
# def make_excel_list(data, column_names):
#     list_data = []
#     list_data.append(column_names)
#     for item in data:
#         tmp = [item.htsn, item.name, item.project, item.aname, item.bname, item.filename, item.filenum, item.num,
#                item.dfnum, item.cwnum, item.cdnum, item.creater.nickname, item.creater.company, item.creater.department]
#         list_data.append(tmp)
#     return list_data
#
#
# excel.init_excel(app)
#
#
# @seal_page.route("/export", methods=['GET'])
# def export_records():
#     starttime = request.args.get('starttime')
#     endtime = request.args.get('endtime')
#
#     query_sets = SealOrder.query.filter(
#         SealOrder.created_time.between(starttime, endtime), SealOrder.type == "合同", SealOrder.mark == 2).order_by(
#         SealOrder.created_time.desc()).all()
#     column_names = ['合同编号', '合同名称', '项目', '甲方名称', '乙方名称', '附件名称', '合同页数', '总分数', '对方份数', '财务份数', '存档份数', '申请人', '申请公司',
#                     '申请部门']
#     excel_data = make_excel_list(query_sets, column_names)
#
#     # app.logger.info(excel_data)
#
#     return excel.make_response_from_array(excel_data, file_type='xls',
#                                           file_name="导出数据",
#                                           sheet_name='合同信息')
#
# # classone = ["招标阶段", "投标阶段", "谈标阶段", "定标阶段", "合同阶段"]
# # num = []
# # for item in classone:
# #     datanum = BookDetail.query.filter_by(pid=1, classone=item).count()
# #     num.append(datanum)
# # print(num)